<?php

namespace App\Http\Controllers\Api;

use App\Blog;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class BlogController extends Controller
{
    /**
     * 根目录按时间由新到旧获取6篇文章，使用Paginate控制数量
     * 前端添加“查看更多”按钮来分页
     *
     * 根据查询参数 type 不同切换选择模式
     * 可选值有"hot","recommend","zone","detail"
     * 分别对应热门文章、精品推荐、交流圈查询
     *
     * 其中 zone 模式需要第二个查询参数 zid
     *      detail                     blog_id
     */
    public function index(Request $request)
    {
        switch ($request->type) {
            case 'hot':
                return Blog::where('like_num', '>=', '50')->Paginate(6);
                break;
            case 'recommend':
                return Blog::where('recommend', 1)->get();
                break;
            case 'zone':
                return Blog::where('zone_id', $request->zid)->Paginate(6);
                break;
            case 'craftsman':
                return Blog::where('craftsman_id', $request->cid)->Paginate(6);
                break;
            case 'detail':
                return Blog::with(['zone'])->find($request->blog_id);
                break;
            default:
                return DB::table('blog')->latest('post_time')->Paginate(6);
                break;
        }
    }

    public function upload(Request $request, $uid = 0)
    {
        $file = $request->file('file');
        $clientName = $_FILES['file']['name'];
        $extension = explode('.', $clientName);
        $newName = md5(date('ymdhis') . $clientName) . "." . $extension[1];
        if ($request->uid) {
            $file->move("storage/blog/{$request->uid}", $newName);
            return Str::start($newName, "http://craftsman.test/storage/blog/{$request->uid}/");
        } else {
            $file->move("storage/blog/common", $newName);
            return Str::start($newName, "http://craftsman.test/storage/blog/common/");
        }
    }

    /**
     * body 带id字段为更新
     * 否则为新建
     */
    public function updateOrCreate(Request $request)
    {
        if ($request->id) { // 更新原记录
            Blog::find($request->id)->update([
                'title' => $request->title,
                'cover' => $request->cover,
                'content' => $request->content,
            ]);
            return response()->json([
                'status' => 'success',
                'status_code' => 200,
            ]);
        }

        // 创建新纪录
        return Blog::create([
            'craftsman_id' => $request->cid,
            'zone_id' => $request->zid,
            'title' => $request->title,
            'cover' => $request->cover,
            'content' => $request->content,
            'post_time' => now()->addHour(8),
        ]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Blog  $blog
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        $blog = Blog::find($id);
        $oldFile = Str::after($blog->cover, "http://craftsman.test/storage/");
        Storage::delete(Str::finish("public/", $oldFile));
        $blog->delete();
        return 'OK';
    }
}
